package com.livepass.android.girlspicks.adapters;

import java.util.ArrayList;

import com.livepass.android.girlspicks.helps.ContentProviderDB;
import com.livepass.android.girlspicks.models.Article;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class ArticleTableAdapter {
	//public static final String prefix = "article";
	public String TAG = "TRADUCTION";
	public static final int LIMIT = 50;

	// Article table name
	public static final String TABLE_NAME = "article";
	// Article Table Columns names
	public static final String ARTICLE_ID = "article_id";
	public static final String TITLE = "title";
	public static final String ORG_SITENAME = "site";
	public static final String CMT_COUNT = "cmt_count";
	public static final String IMG_URL = "image";
	public static final String ORG_URL = "url";
	public static final String OVERVIEW = "overview";
	public static final String IS_BOOKMARK = "is_bookmark";

	private Context mContext;
	private static ArticleTableAdapter mAdapter;

	// Database creation SQL statement
	public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + ARTICLE_ID + " text Collate UNICODE ,"
			+ TITLE + " text Collate UNICODE ,"
			+ ORG_SITENAME + " text Collate UNICODE ,"
			+ CMT_COUNT + " text Collate UNICODE ,"
			+ IMG_URL + " text Collate UNICODE ,"
			+ ORG_URL + " text Collate UNICODE ,"
			+ OVERVIEW + " text Collate UNICODE ,"
			+ IS_BOOKMARK + " text Collate UNICODE" + ")";

	private ArticleTableAdapter(Context context) {
		mContext = context;
	}

	public static ArticleTableAdapter getInstance(Context context) {
		if (mAdapter == null) {
			mAdapter = new ArticleTableAdapter(context.getApplicationContext());
		}
		return mAdapter;
	}

	public void addArticle(ContentValues values) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		mContext.getContentResolver().insert(contentUri, values);
	}

	public String getArticleId(String articleId, String defaultValues) {
		String value = defaultValues;
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor cursor = mContext.getContentResolver().query(contentUri,
				new String[] { ARTICLE_ID }, ARTICLE_ID + "=?",
				new String[] { articleId }, null);
		if (cursor.moveToFirst()) {
			value = cursor.getString(0);
		}
		cursor.close();
		return value;
	}

	public String getArticleId(String key) {
		String value = null;
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor cursor = mContext.getContentResolver().query(contentUri,
				new String[] { IS_BOOKMARK }, ARTICLE_ID + "=?",
				new String[] { key }, null);
		if (cursor.moveToFirst()) {
			value = cursor.getString(0);
		}
		cursor.close();
		return value;
	}
	
	public ArrayList<Article> getAllArticle() {
		ArrayList<Article> listArticle = new ArrayList<Article>();
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor cursor = mContext.getContentResolver().query(contentUri,
				null, IS_BOOKMARK + "=?",
				new String[] { "1" }, null);
		Article article;
		while (cursor.moveToNext()) {
			article = new Article();
			article.setArticleId(cursor.getString(cursor.getColumnIndex("article_id")));
			article.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			article.setSite(cursor.getString(cursor.getColumnIndex("site")));
			article.setCmtCount(cursor.getString(cursor.getColumnIndex("cmt_count")));
			article.setImage(cursor.getString(cursor.getColumnIndex("image")));
			article.setUrl(cursor.getString(cursor.getColumnIndex("url")));
			article.setOverview(cursor.getString(cursor.getColumnIndex("overview")));
			listArticle.add(article);
		}
		cursor.close();
		return listArticle;
	}
	
	/**
	 * update
	 * @param key
	 * @return
	 */
	public int updateArticle(String id, ContentValues values) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int rows = mContext.getContentResolver().update(contentUri, values,
				ARTICLE_ID+"=?",
				new String[] { id });
		return rows;
	}

	public int getCount() {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor cursor = mContext.getContentResolver().query(contentUri,
				new String[] { ARTICLE_ID }, null, null, null);
		int count = cursor.getCount();
		cursor.close();
		return count;
	}

	public int clear() {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int count = mContext.getContentResolver()
				.delete(contentUri, null, null);
		return count;
	}

}
